#include "ekran_flag.h"
#include "ui_ekran_flag.h"

#include <QJsonArray>
#include <QScreen>

ekran_flag::ekran_flag(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ekran_flag)
{
    ui->setupUi(this);
    this->setWindowTitle("ИСAП-экран флажки");
    settings = new QSettings(ORGANIZATION_NAME, APPLICATION_NAME);
    groupsGrades[0] = ui->hFlagsJudges;
    if(QApplication::screens().size() > 1) {
        QScreen* screen = QApplication::screens().at(1);
        QSize size = screen->availableSize();
        wEkran = (size.width()-50)/2;
        ui->sportBlue->setFixedWidth(wEkran);
        ui->sportRed->setFixedWidth(wEkran);
    }
}

ekran_flag::~ekran_flag()
{
    delete ui;
}

//Вывод выступающего, категории, пхумсэ
void ekran_flag::recieveDataFlag(QJsonObject json) {
    QStringList list = json["sportsmen"].toString().split(";");

    ui->sportBlue->setText("0");
    ui->sportRed->setText("0");
    if(list.count() == 4) ui->phumseOli->setText("1: " + list[2] + "\n2: " + list[3]);
     if(list.count() == 3) ui->phumseOli->setText("1: " + list[2]);
    ui->sportBlue->setText(list[0]);
    ui->sportRed->setText(list[1]);

    ui->totalBlue->setText("0");
    ui->totalRed->setText("0");

    ui->catOli->setText(json["kategoria"].toString());
    countJudges = settings->value("SETTINGS_KSUD").toInt();
    if (show == 1) {
        for(int i = 0; i < countJudges; i++) {
            flagsBtn[i]->deleteLater();
            show = 0;
        }
    }
    QFontMetrics fmBlue(ui->sportBlue->font());
    QFontMetrics fmRed(ui->sportRed->font());
    int pixelsWideBlue = fmBlue.horizontalAdvance(ui->sportBlue->text());
    int pixelsWideRed = fmRed.horizontalAdvance(ui->sportRed->text());
    if (tmrLine != NULL) {
        tmrLine->stop();
        delete tmrLine;
    }
    if (pixelsWideBlue > wEkran || pixelsWideRed > wEkran) {
        tmrLine = new QTimer(this);
        tmrLine->setInterval(1000);
        connect(tmrLine, SIGNAL(timeout()), this, SLOT(runLine()));
        tmrLine->start();
    }
}

void ekran_flag::runLine() {
    const int lengthBlue = ui->sportBlue->text().length();
    const int lengthRed = ui->sportRed->text().length();

    if(++m_shift >= lengthBlue || ++m_shift >= lengthRed)
      m_shift = 0;

    ui->sportBlue->setText(ui->sportBlue->text().right(m_shift) + ui->sportBlue->text().left(lengthBlue - m_shift));
    ui->sportRed->setText(ui->sportRed->text().right(m_shift) + ui->sportRed->text().left(lengthRed - m_shift));
}

void ekran_flag::recieveOcenFlag(QJsonObject flagsObject) {
    QFont font;
    font.setPixelSize(50);
    QJsonValue jv;
    QJsonArray ja;
    QJsonObject subtree;
    if(show == 0) {
        for(int i = 0; i < countJudges; i++) {
            flagsBtn[i] = new QPushButton();
            flagsBtn[i]->setFixedSize(300,100);
            flagsBtn[i]->setText(QString::number(i+1));
            flagsBtn[i]->setFont(font);
            flagsBtn[i]->setStyleSheet("color: yellow;");
            groupsGrades[0]->addWidget(flagsBtn[i]);
        }
    }
    show = 1;
    for (int i=0; i<flagsObject.count(); i++) {
        jv = flagsObject.value("ocenki"+QString::number(i));
        if(jv.isArray()) {
            ja = jv.toArray();
            subtree = ja.at(0).toObject();
            if (subtree["ocenki_t"].toInt() == 1) flagsBtn[subtree["ocenki_nomsud"].toInt()-1]->setStyleSheet("background-color: blue;");
            if (subtree["ocenki_p"].toInt() == 1) flagsBtn[subtree["ocenki_nomsud"].toInt()-1]->setStyleSheet("background-color: red;");
        }
    }
    ui->totalBlue->setText(QString::number(flagsObject["totalBlue"].toInt()));
    ui->totalRed->setText(QString::number(flagsObject["totalRed"].toInt()));

}
